package com.skd.mall.product.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.skd.common.utils.R;
import com.skd.mall.product.entity.SkuImagesEntity;
import com.skd.mall.product.entity.SpuInfoDescEntity;
import com.skd.mall.product.fegin.SeckillFeginService;
import com.skd.mall.product.service.*;
import com.skd.mall.product.vo.ItemVO;
import com.skd.mall.product.vo.SeckillVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skd.common.utils.PageUtils;
import com.skd.common.utils.Query;

import com.skd.mall.product.dao.SkuInfoDao;
import com.skd.mall.product.entity.SkuInfoEntity;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuInfoDao skuInfoDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        // 检索关键字
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            wrapper.and(w->{
                w.eq("sku_id",key).or().like("sku_name",key);
            });
        }

        // 分类
        String catalogId = (String)params.get("catalogId");
        if(!StringUtils.isEmpty(catalogId) && !"0".equalsIgnoreCase(catalogId)){
            wrapper.eq("catalog_id",catalogId);
        }
        // 品牌
        String brandId = (String)params.get("brandId");
        if(!StringUtils.isEmpty(brandId) && !"0".equalsIgnoreCase(brandId)){
            wrapper.eq("brand_id",brandId);
        }
        // 价格区间
        String min = (String) params.get("min");
        if(!StringUtils.isEmpty(min)){
            wrapper.ge("price",min);
        }
        String max = (String) params.get("max");
        if(!StringUtils.isEmpty(max)){
            try {
                // 如果max=0那么我们也不需要加这个条件
                BigDecimal bigDecimal = new BigDecimal(max);
                if(bigDecimal.compareTo(new BigDecimal(0)) == 1){
                    // 说明 max > 0
                    wrapper.le("price",max);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public String getNameById(Long skuId) {
        SkuInfoEntity skuInfoEntity = skuInfoDao.selectOne(
                new QueryWrapper<SkuInfoEntity>().eq("sku_id", skuId)
        );
        return skuInfoEntity.getSkuName();
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        List<SkuInfoEntity> skus = skuInfoDao.selectList(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        return skus;
    }

    @Override
    public SkuInfoEntity getSkuBySkuId(Long skuId) {
        SkuInfoEntity entity = this.getOne(
                new QueryWrapper<SkuInfoEntity>().eq("sku_id", skuId));
        return entity;
    }


    /**
     *
     *
     * @param skuId
     * @return
     */

    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    private SpuInfoDescService spuInfoDescService;
    @Autowired
    private ProductAttrValueService productAttrValueService;
    @Autowired
    private SeckillFeginService seckillFeginService;
    @Override
    public ItemVO getItemVOBySkuId(Long skuId) throws ExecutionException, InterruptedException {
        ItemVO itemVO = new ItemVO();

        CompletableFuture<Void> skuInfoFuture = CompletableFuture.runAsync(() -> {
            // 1.sku的基本信息 pms_sku_info
            SkuInfoEntity skuInfoEntity = this.getSkuBySkuId(skuId);
            itemVO.setInfo(skuInfoEntity);
        });
        CompletableFuture<Void> imagesFuture = CompletableFuture.runAsync(()->{
            // 2.sku的图片信息pms_sku_images
            List<SkuImagesEntity> images = skuImagesService.getSkuImagesBySkuId(skuId);
            itemVO.setImages(images);
        });
        CompletableFuture<Void> saleFuture = CompletableFuture.runAsync(()->{
            // 3.获取spu中的销售属性的组合
            List<ItemVO.SkuItemSaleAttrVo> saleAttrs = skuSaleAttrValueService.getSaleAttrsBySkuId(skuId);
            itemVO.setSaleAttrs(saleAttrs);
        });
        CompletableFuture<Void> descFuture = CompletableFuture.runAsync(()->{
            // 4.获取SPU的介绍
            SpuInfoDescEntity descEntity = spuInfoDescService.getSpuDescBySkuId(skuId);
            itemVO.setDesc(descEntity);
        });
        CompletableFuture<Void> baseFuture = CompletableFuture.runAsync(() -> {
            // 5.获取SPU的规格参数
            List<ItemVO.SpuItemGroupAttrVo> baseAttrs = productAttrValueService.getproductAttrValueBySkuId(skuId);
            itemVO.setBaseAttrs(baseAttrs);
        });
        CompletableFuture<Void> seckillFuture = CompletableFuture.runAsync(()->{
            //6绑定秒杀信息
            R r = seckillFeginService.getSeckillSessionBySkuId(skuId);
            String s = (String) r.get("data");
            SeckillVO seckillVO = JSON.parseObject(s, SeckillVO.class);
            itemVO.setSeckillVO(seckillVO);
        });
        CompletableFuture.allOf(skuInfoFuture,imagesFuture,saleFuture,descFuture,baseFuture,seckillFuture).get();
        return itemVO;
    }

    @Override
    public List<String> getAttrsBySkuId(Long skuId) {
        List<String> list = skuInfoDao.getAttrs(skuId);
        return list;
    }
}